import axios from "axios";
import {Message} from 'element-ui';
import router from '@/router/index'

const instance = axios.create({
    baseURL: 'http://dyw770.top:18080',
    timeout: 60 * 1000,
});

// 请求拦截
instance.interceptors.request.use(config => {

        // 请求携带token
        const token = localStorage.getItem("edb-authorization-token")
        if (token && !config.url.endsWith("/login")) {
            config.headers["Authorization"] = "Bearer " + token
        }
        return config
    },
    error => {
        // console.log(error)
        return Promise.reject(error)
    }
);

// 响应拦截
instance.interceptors.response.use(
    res => {
        const data = res.data

        switch (data.code) {
            case 1001:
                Message({
                    message: data.msg,
                    type:'error'
                })
                break
            case 2000:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2001:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2002:
                localStorage.removeItem('edb-authorization-token')
                localStorage.removeItem('admin')
                router.push('/login')
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2003:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2004:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2005:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2006:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 2007:
                localStorage.removeItem('edb-authorization-token')
                localStorage.removeItem('admin')
                router.push('/login')
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 3000:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 4000:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 5002:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 5003:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 5004:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 6001:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
            case 6002:
                Message({
                    message: data.msg,
                    type: 'error'
                })
                break
        }

        return res
    },
    error => {
        return Promise.reject(error)
    }
);

export default instance;